/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package backing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import officeboost.domain.GroupService;
import officeboost.domain.PersonService;
import officeboost.entity.User;

/**
 *
 * @author XJ
 */
@ManagedBean
public class MemberAdminBacking {

    @EJB
    private PersonService personService;
    @EJB 
    private GroupService groupService;
    private List<User> userList=new ArrayList<>();
    private String groupname;
    private String ID;
    private int searchBy=0;
    private String chooseGroup;
    Map mapchoosegroup=new HashMap();
    
    //-------------------------------
    private String IDtest;
    private String grouptest;

    public String getIDtest() {
        return IDtest;
    }

    public void setIDtest(String IDtest) {
        this.IDtest = IDtest;
    }

    public String getGrouptest() {
        return grouptest;
    }

    public void setGrouptest(String grouptest) {
        this.grouptest = grouptest;
    }
    
    
    //-------------------------------
    public Map getMapchoosegroup() {
        return mapchoosegroup;
    }

    public void setMapchoosegroup(Map mapchoosegroup) {
        this.mapchoosegroup = mapchoosegroup;
    }
    
    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getGroupname() {
        return groupname;
    }

    public void setGroupname(String groupname) {
        this.groupname = groupname;
    }

    public String getChooseGroup() {
        return chooseGroup;
    }

    public void setChooseGroup(String chooseGroup) {
        this.chooseGroup = chooseGroup;
    }
    
    
    public void init(){
       if(searchBy==1)
       {
           this.searchByGroup();
       }
       else if(searchBy==2)
       {
           this.searchByID();
       }
       else this.searchAll();
    }
    
    public String searchAll(){
        
        this.setUserList(personService.findAll());
        this.searchBy=0;
        return "member";
    }
    
    public String searchByGroup(){
        List<User> tmp=new ArrayList<>();        
//        if(this.groupname!=null&&groupService.findUsersByGroup(this.groupname))
//        {
//             
//        
//        }
       if(groupService.getUsersByGruopName(this.groupname)!=null)
           this.userList=groupService.getUsersByGruopName(this.groupname);
        this.searchBy=1;
        return "member";
    }
     public String searchByID(){ 
         List<User> tmp=new ArrayList<>();
         if(this.ID!=null&&personService.find(this.ID)!=null)
         {
        
            tmp.add(personService.find(this.ID));
            this.setUserList(tmp);
         }
        
         this.searchBy=2;
         return "member";
     }
     public String addToGroup(String id){  
         //groupService.addUserToGroup(id, this.chooseGroup);
         groupService.addUserToGroup(this.IDtest,this.grouptest);
         this.searchBy=1;
         return "member";
     }
     public String deleteFromGroup(String s){
         groupService.deleteUserFromGroup(this.IDtest, this.grouptest);
         this.searchBy=1;
         return "member";
     }
     
     public void chooseGroup(String s){
         
        this.mapchoosegroup.put(s, this.chooseGroup);
     }
}
